{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Animated cartoons of active anomaly detection\n",
    "\n",
    "This notebook demonstrates how different anomaly detection methods selects outliers in a dataset.\n",
    "We will use a 2-D toy dataset and two methods: standard Isolation Forest and active Pine Forest."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-14T10:30:08.794882Z",
     "start_time": "2024-03-14T10:30:08.619178Z"
    }
   },
   "outputs": [],
   "source": [
    "import io\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.patches as mpatches\n",
    "from matplotlib.figure import Figure\n",
    "from matplotlib.backends.backend_agg import FigureCanvas  # noqa\n",
    "\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-14T10:30:09.323528Z",
     "start_time": "2024-03-14T10:30:08.795857Z"
    }
   },
   "outputs": [],
   "source": [
    "from coniferest.datasets import non_anomalous_outliers, Label\n",
    "from coniferest.session.oracle import create_oracle_session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-14T10:30:09.329241Z",
     "start_time": "2024-03-14T10:30:09.324127Z"
    }
   },
   "outputs": [],
   "source": [
    "class CartoonDrawer:\n",
    "    COLORS = {Label.ANOMALY: 'red', Label.UNKNOWN: 'grey', Label.REGULAR: 'blue'}\n",
    "\n",
    "    def __init__(self, session, title=None):\n",
    "        \"\"\"\n",
    "        Drawing cartoons with anomaly detection.\n",
    "        \"\"\"\n",
    "        self.title = title\n",
    "        self.trajectory = np.array(list(session.known_labels.keys()))\n",
    "        self.data_features = session._data\n",
    "        self.data_labels = session._metadata\n",
    "\n",
    "\n",
    "    def draw_cartoon(self):\n",
    "        \"\"\"\n",
    "        Draw a animation how regressor performs.\n",
    "\n",
    "        Returns\n",
    "        -------\n",
    "        List of PIL images.\n",
    "        \"\"\"\n",
    "\n",
    "        data_features = self.data_features\n",
    "        data_labels = self.data_labels\n",
    "        COLORS = self.COLORS\n",
    "\n",
    "        images = []\n",
    "        for i in range(len(self.trajectory)):\n",
    "            fig = Figure()\n",
    "            canvas = FigureCanvas(fig)\n",
    "\n",
    "            ax = fig.subplots()\n",
    "            title = self.title and f'{self.title}, iteration {i}'\n",
    "            ax.set(title=title, xlabel='x1', ylabel='x2')\n",
    "\n",
    "            ax.scatter(*data_features.T, color=COLORS[Label.REGULAR], s=10)\n",
    "            # We don't have trace any more\n",
    "            # ax.scatter(*data_features[trace, :].T, color=COLORS[Label.ANOMALY], s=10)\n",
    "\n",
    "            prehistory = self.trajectory[:i]\n",
    "            index = data_labels[prehistory] == Label.ANOMALY\n",
    "            if np.any(index):\n",
    "                ax.scatter(*data_features[prehistory[index], :].T, marker='*', color=COLORS[Label.ANOMALY], s=80)\n",
    "\n",
    "            index = ~index\n",
    "            if np.any(index):\n",
    "                ax.scatter(*data_features[prehistory[index], :].T, marker='*', color=COLORS[Label.REGULAR], s=80)\n",
    "\n",
    "            ax.scatter(*data_features[self.trajectory[i], :].T, marker='*', color='k', s=80)\n",
    "\n",
    "            normal_patch = mpatches.Patch(color=COLORS[Label.REGULAR], label='Regular')\n",
    "            anomalous_patch = mpatches.Patch(color=COLORS[Label.ANOMALY], label='Anomalous')\n",
    "            ax.legend(handles=[normal_patch, anomalous_patch], loc='lower left')\n",
    "\n",
    "            canvas.draw()\n",
    "            size = (int(canvas.renderer.width), int(canvas.renderer.height))\n",
    "            s = canvas.tostring_rgb()\n",
    "            image = Image.frombytes('RGB', size, s)\n",
    "\n",
    "            images.append(image)\n",
    "            del canvas\n",
    "            del fig\n",
    "\n",
    "        return images\n",
    "\n",
    "    def save_cartoon(self, file):\n",
    "        \"\"\"\n",
    "        (Draw and) save a cartoon.\n",
    "\n",
    "        Parameters\n",
    "        ----------\n",
    "        file\n",
    "            Filename or file object to write GIF file to.\n",
    "\n",
    "        Returns\n",
    "        -------\n",
    "        None\n",
    "        \"\"\"\n",
    "        images = self.draw_cartoon()\n",
    "        images[0].save(file, format='GIF',\n",
    "                       save_all=True, append_images=images[1:],\n",
    "                       optimize=False, duration=500, loop=0)\n",
    "\n",
    "    def display_cartoon(self):\n",
    "        \"\"\"\n",
    "        IPython display of the drawn GIF.\n",
    "\n",
    "        Returns\n",
    "        -------\n",
    "        None\n",
    "        \"\"\"\n",
    "        import IPython.display\n",
    "\n",
    "        with io.BytesIO() as buffer:\n",
    "            self.save_cartoon(buffer)\n",
    "            return IPython.display.Image(buffer.getvalue())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-14T10:30:09.560022Z",
     "start_time": "2024-03-14T10:30:09.329824Z"
    }
   },
   "outputs": [],
   "source": [
    "data, labels = non_anomalous_outliers(inliers=1000, outliers=50)\n",
    "\n",
    "plt.figure()\n",
    "plt.title('Data overview')\n",
    "plt.scatter(*data[labels == 1, :].T, color='blue', s=10, label='Normal')\n",
    "plt.scatter(*data[labels == -1, :].T, color='red', s=10, label='Anomaluous')\n",
    "plt.xlabel('x1')\n",
    "plt.ylabel('x2')\n",
    "plt.legend()\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-14T10:30:17.082668Z",
     "start_time": "2024-03-14T10:30:09.573590Z"
    }
   },
   "outputs": [],
   "source": [
    "from coniferest.isoforest import IsolationForest\n",
    "\n",
    "session_isoforest = create_oracle_session(\n",
    "    data=data,\n",
    "    labels=labels,\n",
    "    model=IsolationForest(random_seed=0),\n",
    ").run()\n",
    "\n",
    "drawer = CartoonDrawer(session_isoforest, title='Isolation Forest')\n",
    "drawer.display_cartoon()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from coniferest.pineforest import PineForest\n",
    "\n",
    "session_pineforest = create_oracle_session(\n",
    "    data=data,\n",
    "    labels=labels,\n",
    "    model=PineForest(random_seed=0),\n",
    ").run()\n",
    "\n",
    "drawer = CartoonDrawer(session_pineforest, title='Pine Forest')\n",
    "drawer.display_cartoon()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def perfcurve(session):\n",
    "    return np.cumsum(np.fromiter(session.known_labels.values(), dtype=int) == Label.A)\n",
    "\n",
    "plt.figure()\n",
    "plt.title('AD performance curves')\n",
    "plt.plot(perfcurve(session_isoforest), label='Isolation Forest')\n",
    "plt.plot(perfcurve(session_pineforest), label='Pine Forest')\n",
    "plt.axhline(sum(labels == Label.A), color='grey')\n",
    "plt.xlabel('number of iteration')\n",
    "plt.ylabel('true anomalies detected')\n",
    "plt.grid()\n",
    "plt.legend()\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
